package com.cast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cast.constant.MessageConstant;
import com.cast.entity.PageResult;
import com.cast.entity.QueryPageBean;
import com.cast.entity.Result;
import com.cast.pojo.Address;
import com.cast.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @项目名称 health
 * @包名 com.cast.controller
 * @类名 AddressController
 * @作者 lx
 * @时间 2020/3/6 22:16
 * @描述
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){

        Integer currentPage = queryPageBean.getCurrentPage();

        if (currentPage == null || currentPage == 0){
            currentPage =1;
            queryPageBean.setCurrentPage(currentPage);
        }

        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null || pageSize == 0){
            pageSize =10;
            queryPageBean.setPageSize(pageSize);
        }

        PageResult pageResult = addressService.findByPage(queryPageBean);

        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        try {
            addressService.add(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Address address = addressService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address){
        try {
            addressService.edit(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            addressService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            // 服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
}
